//
//  UIView+Extension.m
//  BBBS
//
//  Created by 👄 on 15/7/31.
//  Copyright (c) 2015年 sczy. All rights reserved.
//
#import "UIView+WExtension.h"

@implementation UIView (Extension)
-(void)setOrigin_wl:(CGPoint)origin_wl{
    CGRect frame = self.frame;
    frame.origin = origin_wl;
    self.frame = frame;
}
-(CGPoint)origin_wl{
    return self.frame.origin;
}

-(void)setSize_wl:(CGSize)size_wl{
    CGRect frame = self.frame;
    frame.size = size_wl;
    self.frame = frame;
}
-(CGSize)size_wl{
    return self.frame.size;
}

-(void)setX_wl:(CGFloat)x_wl{
    CGRect frame = self.frame;
    frame.origin.x = x_wl;
    self.frame = frame;
}

-(CGFloat)x_wl{
    return self.frame.origin.x;

}

-(void)setCenterX_wl:(CGFloat)centerX_wl{
    CGPoint center = self.center;
    center.x = centerX_wl;
    self.center = center;
}
- (CGFloat)centerX_wl
{
    return self.center.x;
}
-(void)setCenterY_wl:(CGFloat)centerY_wl{
    CGPoint center = self.center;
    center.y = centerY_wl;
    self.center = center;
}
- (CGFloat)centerY_wl
{
    return self.center.y;
}


-(void)setY_wl:(CGFloat)y_wl{
    CGRect frame = self.frame;
    frame.origin.y = y_wl;
    self.frame = frame;
}
- (CGFloat)y_wl
{
    return self.frame.origin.y;
}

-(void)setWidth_wl:(CGFloat)width_wl{
    CGRect frame = self.frame;
    frame.size.width = width_wl;
    self.frame = frame;
}

- (CGFloat)width_wl
{
    return self.frame.size.width;
}
-(void)setHeight_wl:(CGFloat)height_wl{
    CGRect frame = self.frame;
    frame.size.height = height_wl;
    self.frame = frame;
}


- (CGFloat)height_wl
{
    return self.frame.size.height;
}

-(CGFloat)maxX_wl{
    return self.frame.origin.x + self.frame.size.width;

}
-(CGFloat)maxY_wl{
    return self.frame.origin.y + self.frame.size.height;

}

-(void)setMaxX_wl:(CGFloat)maxX_wl{
    CGRect frame = self.frame;
    frame.origin.x = maxX_wl - frame.size.width;
    self.frame = frame;
}
-(void)setMaxY_wl:(CGFloat)maxY_wl{
    CGRect frame = self.frame;
    frame.origin.y = maxY_wl - frame.size.height;
    self.frame = frame;
}




/*
 * 当前这个View是否和keyWindow有重叠
 */
- (BOOL)isShowingOnKeyWindow_wl
{
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    // 以主窗口左上角为坐标原点, 计算self的矩形框
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    CGRect winBounds = keyWindow.bounds;
    
    // 主窗口的bounds 和 self的矩形框 是否有重叠
    BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
    
    return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
}
/*
 * 当前这个View是否 全部显示在 keyWindow上
 */
- (BOOL)isShowingAllOnKeyWindowBol_wl
{
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    // 以主窗口左上角为坐标原点, 计算self的矩形框
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    CGRect winBounds = keyWindow.bounds;
    
    CGRect intersectionRect = CGRectIntersection(newFrame, winBounds);
    
    BOOL w = intersectionRect.size.width >= newFrame.size.width;
    
    BOOL h = intersectionRect.size.height >= newFrame.size.height;
    
    return w && h;
}
/*
 * 当前这个View是否 全部显示在 keyWindow上 如果是 则返回 CGRectZero 否则返回大小
 */
- (CGRect)isShowingAllOnKeyWindow_wl
{
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    // 以主窗口左上角为坐标原点, 计算self的矩形框
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    CGRect winBounds = keyWindow.bounds;
    
    CGRect intersectionRect = CGRectIntersection(newFrame, winBounds);
    
    BOOL w = intersectionRect.size.width >= newFrame.size.width;
    
    BOOL h = intersectionRect.size.height >= newFrame.size.height;
    
    if (w && h) {
        return CGRectZero;
    }
    else{
        return CGRectMake(0, 0, intersectionRect.size.width - newFrame.size.width, intersectionRect.size.height - newFrame.size.height);
    }
    
}
// 从xib加载
+ (instancetype)wl_viewFromXib
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] firstObject];
}



@end
